﻿using ETA.ClientService;
using ETA.Common;
using ETA.Entity;
using ETA.IService;
using ETA.Web.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    /// <summary>
    /// 个人中心 非游客可进入
    /// </summary>
    [AuthFilter("实习生")]
    public class IController : BaseController
    {
        private static IWebMessageService _IWebMessageService { set; get; }
        private static IUserService _IUserService { set; get; }

        public IController(IWebMessageService IWebMessageService, IUserService service)
        {
            _IWebMessageService = IWebMessageService;
            _IUserService = service;
        }

        //
        // GET: /I/

        public ActionResult Index()
        {
            var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId); ;
            return View(user);
        }

        public ActionResult Message()
        {
            var messageList = new List<WebMessage>();
            var userId = CurrentUser.UserId;
            var queryRes = _IWebMessageService.QueryWebMessageList(new QueryWebMessageRequest
            {
                WebMessage = new Entity.WebMessage
                {
                    UserId = userId
                }
            });
            if (queryRes.Result.IsSuccess)
            {
                messageList = queryRes.WebMessageList.ToList();
            }
            return View(messageList);
        }


        public ActionResult ReadMessage(string msgId)
        {
            var res = new Response();
            var queryRes = _IWebMessageService.QueryWebMessageList(new QueryWebMessageRequest
            {
                WebMessage = new Entity.WebMessage
                {
                    WebMessageId = msgId
                }
            });
            if (queryRes.Result.IsSuccess)
            {
                var msg = queryRes.WebMessageList.First();
                if (msg != null)
                {
                    msg.MessageOpenEnum = DomainEnum.MessageOpenEnum.已读;
                    msg.ModifyDate = DateTime.Now;
                    res = _IWebMessageService.ModifyWebMessageService(new ModifyWebMessageRequest
                    {
                        WebMessage = msg
                    });
                }
            }
            return JsonWithDate(res);
        }

        public ActionResult ModifyPwd(string OldPwd, string NewPwd, string NewPwdCheck)
        {
            var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);

            var dencryptPwd = CommHelper.Base64Dencrypt(user.Password);
            var modifyUserResponse = new ModifyUserResponse();
            var err = string.Empty;

            if (NewPwd.Trim() != NewPwdCheck.Trim())
            {
                err += "新密码和密码验证不一致\r";
            }

            if (dencryptPwd != OldPwd.Trim())
            {
                err += "原密码有误,请确认\r";
            }

            if (dencryptPwd == NewPwdCheck.Trim())
            {
                err += "新密码和旧密码一致无须修改\r";
            }

            if (!string.IsNullOrEmpty(err))
            {
                modifyUserResponse.Result.IsSuccess = false;
                modifyUserResponse.Result.ErrMessage = err;
                return JsonWithDate(modifyUserResponse);
            }

            if (ModelState.IsValid)
            {
                user.Password = CommHelper.Base64Encrypt(NewPwd.Trim());
                modifyUserResponse = _IUserService.ModifyUser(new ModifyUserRequest { User = user });
            }
            return JsonWithDate(modifyUserResponse);

        }
    }
}
